home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-01-12 | 3.0 KB | 97 lines | [TEXT/GEOL] |
- Item 4115113 9-Jan-90 07:01
-
- From: POWERUP.DEV Power Up Software,PRT
-
- To: MACAPP.TECH$ MacApp Technical
-
- Sub: UList for circular arrays
-
- Attn: MacApp.Tech$
- SentBy: James Plamondon
- Date 1/8/90
- Subject UList for circular arrays
- From James Plamondon
- To MacApp.Tech$
-
- Subject: UList for circular arrays
- Gentlepersons,
-
- I am faced with the need to write a class to implement circular arrays
- (described, among other places, in the book Data Structures and Algorithms, by
- Aho, Hopcroft, and Ullman, Addison-Wesley, 1982).
-
- In brief, a circular array is one in which the 'virtual' first element may be
- something other than the actual first element, and in which, therefore, a
- given virtual index (i) refers to the actual array element denoted by the
- formula
- actual array index = (i + fStart) MOD fSize;
- given that fSize is the number of elements in the array, fStart is the current
- first element, i is the requested virtual element, and in which both the
- actual and virtual elements are numbered from zero.
-
- I happened to recall that TList is implemented using arrays, and that a method
- of that class, ComputeAddress(), is used to compute the offset to a given
- element. The routine is a simple one, as seen below:
-
- FUNCTION TList.ComputeAddress(index: INTEGER): LONGINT;
-
- VAR
- p: LONGINT;
- x: INTEGER;
- item: LONGINT;
-
- BEGIN
- p := Ord(Handle(SELF)^) + fFirstOffset; { Address of first
- physical item }
-
- IF fDeletions = 0 THEN { If no deletions,
- index to proper element }
- ComputeAddress := p + BSL(index - 1, 2) { p + ((index - 1) * 4)
- }
- ELSE { Ignore deleted
- elements }
- BEGIN
- x := index;
- p := p - kElemSize; { we increment first in
- loop }
-
- WHILE x > 0 DO { Skip elements until
- item is found }
- BEGIN
- p := p + kElemSize;
- item := PLongInt(p)^;
- IF item <> kDeletedElement THEN
- x := x - 1;
- END;
-
- ComputeAddress := p;
- END
- END;
-
- I think that I can use TList to implment circular arrays by adding one field
- (fStart) and overriding one method, ComputeAddress(). fStart would contain
- the current virtual start of the array, as described above. The new version
- of ComputeAddress() would be:
-
- FUNCTION TCircularArray.ComputeAddress(index: INTEGER): LONGINT; OVERRIDE;
- BEGIN
- index := (index + fStart) MOD fSize;
- ComputeAddress := INHERITED ComputeAddress(index);
- END;
-
- Does anybody see any reason why this scheme shouldn't work? Or, does anyone
- know who wrote the TList class, so that I can ask that person directly?
-
- Looking forward to your responses, I am
-
- Yours,
-
- James Plamondon
- Software Engineer
- PowerUp! Software
- 2929 Campus Drive, Suite 300
- San Mateo, CA 94403
- (415) 345-5900 x351
- AppleLink: PowerUp.Eng
-
-